package op3;

import java.util.ArrayList;
import java.util.Scanner;

public class LRU {
    static class Page {
        char num;//页面编号
        int time;//页面没有被访问到的时长
    }
    static ArrayList<Page> pages = new ArrayList<>();//模拟缓冲区的队列
    int size = 4;//设置缓冲区大小为4个页面
    public static void main(String[] args) {
        LRU lru = new LRU();
        lru.initList();
        System.out.println("当前缓冲区大小"+lru.size+"\n缓冲区状态为:");
        lru.printList();
        lru.getPages();
    }

    /**
     * 得到要置换的所有页面序列
     */
    private void getPages() {
        System.out.println("请输入页面序列");
        Scanner in = new Scanner(System.in);
        String str = in.next();
        toChars(str);
    }

    private void toChars(String s) {
        char[] ch = new char[s.length() + 1];
        for (int i = 0; i < s.length(); i++) {
            ch[i] = s.charAt(i);
        }
        ch[s.length()] = '#';
        exchange(ch);
    }

    private void initList() {
        for (int i = 0; i < size; i++) {
            Page page = new Page();
            page.num = '#';
            page.time = 0;
            pages.add(page);
        }
    }

    private void exchange(char[] ch) {
        int i = 0;
        printList();
        while (ch[i++] != '#') {
            System.out.print("\n"+"第"+0+(i+1)+"次置换----");
            if (isIn(ch[i]) != -1) {
                System.out.print("当前页面存在缓冲区:");
                int position = isIn(ch[i]);
                pages.get(position).time = 0;//当前页面时间置为0
                timeAdd(position);//其他的都+1
                printList();
            } else if (space() != -1) {
                System.out.print("缓冲区内有空余页面:");
                int position = space();
                pages.get(position).num = ch[i];
                pages.get(position).time = 0;
                timeAdd(position);
                printList();
            } else if (isIn(ch[i]) == -1) {
                System.out.print("当前页面不在缓冲区:");
                int max = 0;
                int position = 0;
                for (int j = 0; j < size; j++) {
                    if (pages.get(j).time > max) {
                        max = pages.get(j).time;
                        position = j;
                    }
                }
                pages.get(position).num = ch[i];
                pages.get(position).time = 0;
                timeAdd(position);
                printList();
            }
        }
    }

    /**
     * 判断缓冲区是否有空位
     * @return
     */
    private int space() {
        for (int i = 0; i < size; i++) {
            if (pages.get(i).num == '#') {
                return i;
            }
        }
        return -1;
    }

    /**
     * 判断当前页面有没有在缓冲区内，返回位置
     * @param ch
     * @return
     */
    private int isIn(char ch) {
        int flag = -1;
        for (int i = 0; i < size; i++) {
            if (ch != pages.get(i).num) {
                continue;
            }
            flag = i;
            break;
        }
        return flag;
    }

    private void printList() {
        for (int i = 0; i < size; i++) {
            System.out.print(pages.get(i).num + "\t");
        }
    }

    /**
     * 增加页面时间属性
     * @param position
     */
    private void timeAdd(int position) {
        for (int i = 0; i < size; i++) {
            if (i == position || pages.get(i).num == '#') {
                continue;
            }
            pages.get(i).time += 1;
        }
    }
}